/*
*/
CREATE PROC SP_UPDATE_CURRENT_QTR_COST_VALUES ( @BASE_PRODUCT_ID INT )
AS
BEGIN	
	DECLARE @CURR_QTR INT
	DECLARE @CURR_YEAR INT
	DECLARE @QTR_VALUES_CURSOR CURSOR
	DECLARE @PROCESSSTEP_ID INT
	DECLARE @Q4_STD_COST FLOAT
	DECLARE @Q4_YIELD FLOAT

	SET @CURR_QTR = DATEPART(Q, CURRENT_TIMESTAMP)
	SET @CURR_YEAR = DATEPART(YYYY, CURRENT_TIMESTAMP)
	

	IF ( @BASE_PRODUCT_ID IS NULL OR @BASE_PRODUCT_ID <= 0 ) --FOR ALL BASE PRODUCTS
	 BEGIN
		IF ( @CURR_QTR = 1 )
		 BEGIN
			SET @QTR_VALUES_CURSOR = CURSOR FOR SELECT PROCESSSTEP_ID, Q4_STANDARD_COST, Q4_YIELD FROM UPDATE_VIEW_COST_TABLE WHERE YEAR(FIN_YEAR) = (@CURR_YEAR-1)
			OPEN @QTR_VALUES_CURSOR
			FETCH NEXT FROM @QTR_VALUES_CURSOR INTO @PROCESSSTEP_ID, @Q4_STD_COST, @Q4_YIELD
			WHILE ( @@FETCH_STATUS = 0 )
			 BEGIN
				UPDATE UPDATE_VIEW_COST_TABLE SET Q1_STANDARD_COST = @Q4_STD_COST
					WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND PROCESSSTEP_ID = @PROCESSSTEP_ID AND ( Q1_STANDARD_COST IS NULL OR Q1_STANDARD_COST = 0 )
				UPDATE UPDATE_VIEW_COST_TABLE SET Q1_YIELD = @Q4_YIELD 
					WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND PROCESSSTEP_ID = @PROCESSSTEP_ID AND ( Q1_YIELD IS NULL OR Q1_YIELD = 0 )

				SET @PROCESSSTEP_ID = NULL
				SET @Q4_STD_COST = 0
				SET @Q4_YIELD = 0

				FETCH NEXT FROM @QTR_VALUES_CURSOR INTO @PROCESSSTEP_ID, @Q4_STD_COST, @Q4_YIELD
			 END
			CLOSE @QTR_VALUES_CURSOR
			DEALLOCATE @QTR_VALUES_CURSOR
		 END
		ELSE IF ( @CURR_QTR = 2 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q2_STANDARD_COST = Q1_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q2_STANDARD_COST IS NULL OR Q2_STANDARD_COST = 0 )
			UPDATE UPDATE_VIEW_COST_TABLE SET Q2_YIELD = Q1_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q2_YIELD IS NULL OR Q2_YIELD = 0 )
		 END
		ELSE IF ( @CURR_QTR = 3 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q3_STANDARD_COST = Q2_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q3_STANDARD_COST IS NULL OR Q3_STANDARD_COST = 0 )
			UPDATE UPDATE_VIEW_COST_TABLE SET Q3_YIELD = Q2_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q3_YIELD IS NULL OR Q3_YIELD = 0 )
		 END
		ELSE IF ( @CURR_QTR = 4 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q4_STANDARD_COST = Q3_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q4_STANDARD_COST IS NULL OR Q4_STANDARD_COST = 0 )
			UPDATE UPDATE_VIEW_COST_TABLE SET Q4_YIELD = Q3_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q4_YIELD IS NULL OR Q4_YIELD = 0 )
		 END
	 END
	ELSE --ONLY FOR GIVEN BASE PRODUCT
	 BEGIN
		IF ( @CURR_QTR = 1 )
		 BEGIN
			SET @QTR_VALUES_CURSOR = CURSOR FOR SELECT PROCESSSTEP_ID, Q4_STANDARD_COST, Q4_YIELD FROM UPDATE_VIEW_COST_TABLE WHERE YEAR(FIN_YEAR) = (@CURR_YEAR-1) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
			OPEN @QTR_VALUES_CURSOR
			FETCH NEXT FROM @QTR_VALUES_CURSOR INTO @PROCESSSTEP_ID, @Q4_STD_COST, @Q4_YIELD
			WHILE ( @@FETCH_STATUS = 0 )
			 BEGIN
				UPDATE UPDATE_VIEW_COST_TABLE SET Q1_STANDARD_COST = @Q4_STD_COST
					WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND PROCESSSTEP_ID = @PROCESSSTEP_ID AND ( Q1_STANDARD_COST IS NULL OR Q1_STANDARD_COST = 0 )
				UPDATE UPDATE_VIEW_COST_TABLE SET Q1_YIELD = @Q4_YIELD 
					WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND PROCESSSTEP_ID = @PROCESSSTEP_ID AND ( Q1_YIELD IS NULL OR Q1_YIELD = 0 )
				SET @PROCESSSTEP_ID = NULL
				SET @Q4_STD_COST = 0
				SET @Q4_YIELD = 0

				FETCH NEXT FROM @QTR_VALUES_CURSOR INTO @PROCESSSTEP_ID, @Q4_STD_COST, @Q4_YIELD
			 END
			CLOSE @QTR_VALUES_CURSOR
			DEALLOCATE @QTR_VALUES_CURSOR
		 END
		ELSE IF ( @CURR_QTR = 2 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q2_STANDARD_COST = Q1_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q2_STANDARD_COST IS NULL OR Q2_STANDARD_COST = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
			UPDATE UPDATE_VIEW_COST_TABLE SET Q2_YIELD = Q1_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q2_YIELD IS NULL OR Q2_YIELD = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
		 END
		ELSE IF ( @CURR_QTR = 3 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q3_STANDARD_COST = Q2_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q3_STANDARD_COST IS NULL OR Q3_STANDARD_COST = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
			UPDATE UPDATE_VIEW_COST_TABLE SET Q3_YIELD = Q2_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q3_YIELD IS NULL OR Q3_YIELD = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
		 END
		ELSE IF ( @CURR_QTR = 4 )
		 BEGIN
			UPDATE UPDATE_VIEW_COST_TABLE SET Q4_STANDARD_COST = Q3_STANDARD_COST WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q4_STANDARD_COST IS NULL OR Q4_STANDARD_COST = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
			UPDATE UPDATE_VIEW_COST_TABLE SET Q4_YIELD = Q3_YIELD WHERE YEAR(FIN_YEAR) = @CURR_YEAR AND ( Q4_YIELD IS NULL OR Q4_YIELD = 0 ) AND PROCESSSTEP_ID IN ( SELECT PROCESSSTEP_ID FROM PRODUCT WHERE P_PARENT_PRODUCT_ID = @BASE_PRODUCT_ID)
		 END
	 END
END